package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.StorageRecordMapper;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StorageRecordServiceImpl implements StorageRecordService {
    private static final Logger logger = LoggerFactory.getLogger(StorageRecordServiceImpl.class);

    @Autowired
    private StorageRecordMapper storageRecordMapper;

    @Override
    public Page<StorageRecord> getPage(StorageRecord entity, Integer pageNo, Integer pageSize) {
        Page<StorageRecord> page = null;
        try{
            page = new Page<>();
            page.setPageSize(pageSize);
            //查询总页数
            Long totalCount = storageRecordMapper.count(entity);
            page.setTotalCount(totalCount.intValue());
            page.setPageNo(pageNo);
            List<StorageRecord> data = storageRecordMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        }catch (Exception e){
            logger.error("分页查询供应商列表异常", e);
            throw e;
        }
        return page;
    }

    @Override
    public Long count(StorageRecord entity) {
        try{
            return storageRecordMapper.count(entity);
        }catch (Exception e){
            logger.error("查询供应商列表异常", e);
            throw e;
        }
    }

    @Override
    public StorageRecord getByAccount(String account) {
        try{
            return storageRecordMapper.getByAccount(account);
        } catch (Exception e) {
            logger.error("执行StorageRecordServiceImpl.getByAccount(String)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public List<SysUser> getSysUserList(StorageRecord storageRecord) {
        try{
            return storageRecordMapper.getSysUserList(storageRecord);
        } catch (Exception e) {
            logger.error("执行StorageRecordServiceImpl.getSysUserList(StorageRecord)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public boolean upInsert(StorageRecord storageRecord) {
        try{
            if (storageRecord.getId() == null){
                // 添加
                return storageRecordMapper.insert(storageRecord) > 0;
            }else {
                // 修改
                return storageRecordMapper.update(storageRecord) > 0;
            }
        }catch (Exception e){
            logger.error("添加供应商异常", e);
            throw e;
        }
    }

    @Override
    public StorageRecord getById(Long id) {
        try{
            return storageRecordMapper.getById(id);
        } catch (Exception e) {
            logger.error("执行StorageRecordServiceImpl.getById(Integer)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try{
            return storageRecordMapper.deleteById(id) > 0;
        } catch (Exception e) {
            logger.error("执行StorageRecordServiceImpl.deleteById(Integer)方法出现异常", e);
            throw e;
        }
    }
}
